<?php
session_start();
require_once( 'leave_db.php' );
 
$type = new M_leave();
$user_id = $_SESSION['userid'];

if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
      $_type = $_POST['type'];
	  $name = $_POST['name'];
	  $datefrom='';
	  $dateto='';
	  if ($_POST['datefrom'] !=''){
			list($d,$m,$y) = split('/', $_POST['datefrom']);
			$datefrom= $y.'/'.$m.'/'.$d;
		}
		
		 if ($_POST['dateto'] !=''){
			list($d,$m,$y) = split('/', $_POST['dateto']);
			$dateto= $y.'/'.$m.'/'.$d;
		}
	  
      $result=$type->loadData($_type,$name,$datefrom,$dateto);
	 
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id']
								,$row['datefrom'] .' - '. $row['dateto']
								,$row['typename']  
								,$row['name']
								,$row['reason']
								) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
      
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['date_from']="";
		if ($_POST['df'] !=''){
			list($d,$m,$y) = split('/', $_POST['df']);
			$ar['date_from']= $y.'/'.$m.'/'.$d;
		}
		$ar['date_to']="";
		if ($_POST['dt'] !=''){
			list($d,$m,$y) = split('/', $_POST['dt']);
			$ar['date_to']= $y.'/'.$m.'/'.$d;
		}
		
 
		$ar['reason']=  $_POST['reason'];
		$ar['type']=  $_POST['leave'];
		$ar['eid']=  $_POST['emp'];
		 
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		 
		$id=$type->InsertData($ar);
		
    } 
	else if ($_POST['m']=='del'){
         $id=$_POST['id'];
		 $type->deleData($id);
		 
    }/// Age Criteria
	    
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if($_GET['m']=='ly')
			echo json_encode( $type->getLeave());
		else if($_GET['m']=='l')
			echo json_encode( $type->getLeave('Y'));
		else if ($_GET['m']=='emp'){
			echo json_encode( $type->getEmp());
		}
		 
	}
}
 
?>